** Peak-timer 
* all files 
clear
local files : dir "data\in\dta\" files "*.dta"
di `files'

cd "data\in\dta"

foreach file in `files' {
di "`file'"
append using `file' 
}
recast byte noncomplier stratgroups hour 

* drop if ever used > 50 kwh in one hour 
replace forbruk_kwh = 0 if forbruk_kwh == . 
egen maxkwh = max(forbruk_kwh), by(maalepktnr)
drop if maxkwh > 50
drop maxkwh 

*gunique maalepktnr

* take out meters with 3 weeks consecutive 0s. 
fmerge m:1 maalepktnr using "data\in\meters_with_3week0s.dta", keepusing(maalepktnr) keep(master) nogenerate

fmerge m:1 Målepunktnr using "data\in\\kunder_randomisert.dta", keep(match master) keepusing(treat) //nogenerate
tab _merge 
gunique maalepktnr
keep if _merge == 3
drop _merge 
gunique maalepktnr 

*gunique maalepktnr if treat != "Pris"
*gunique maalepktnr if treat == "Pris"
*gunique maalepktnr if treat != "Pris" & lnforbruk != .
*gunique maalepktnr if treat == "Pris" & lnforbruk != .

* Bare peak
*keep if hour >= 16 & hour < 22

keep maalepktnr lnforbruk dato hour treat noncomplier tid stratgroups
g int year = year(dato)
g byte month = month(dato)
g byte day = day(dato)
keep if (year == 2019 & month == 12) | (year == 2020) 
* drop after corona started: 
drop if (month == 3 & day >= 12) | month == 4 
keep if lnforbruk != .

sort maalepktnr tid 
drop if maalepktnr == maalepktnr[_n-1] & tid == tid[_n-1] & lnforbruk == lnforbruk[_n-1]
drop if maalepktnr == maalepktnr[_n-1] & tid == tid[_n-1] 
xtset maalepktnr tid 

merge m:1 dato hour using "data\in\temp_Timesdata2014_2020_inkllags", keep(match) nogen

label var lnforbruk "ln(kwh)"
label var hour "tid på døgnet"
label var hour "time of day"
g byte treatday = 0
  replace treatday = 1 if year == 2019 & month == 12 & day == 10
  replace treatday = 2 if year == 2019 & month == 12 & day == 19
  replace treatday = 3 if year == 2020 & month == 1 & day == 23
  replace treatday = 4 if year == 2020 & month == 1 & day == 30
  replace treatday = 5 if year == 2020 & month == 2 & day == 13
  replace treatday = 6 if year == 2020 & month == 2 & day == 26
  replace treatday = 7 if year == 2020 & month == 3 & day == 5
*  replace treatday = 8 if year == 2020 & month == 3 & day == 31
*  replace treatday = 9 if year == 2020 & month == 4 & day == 28
g trdate = dato if treatday != 0
g byte days_after = 0

foreach d in 21893 21902 21937 21944 21958 21971 21979 {
  replace days_after = 1 if dato == `d' + 1 | dato == `d' + 2 
}
* droppe to dager etter 
*keep if days_after == 0 
*drop days_after 

g byte Peak = inrange(hour,16,21)
g byte NonPeak = 1 - Peak
g byte EventDay = (treatday != 0)
*g byte Treat = treatgroup == 1
g byte Treat = (treat=="Pris")
  drop treat 
g byte TPE = Treat*Peak*EventDay
g byte TNPE = Treat*NonPeak*EventDay
g byte TPnextdays = Treat*Peak*days_after
g byte TNPnextdays = Treat*NonPeak*days_after
g byte shoulder = inrange(hour,14,15) | inrange(hour,22,24)
*g byte nonshoulder = 1 - shoulder
g byte TsE = Treat*shoulder*EventDay
*g byte TnsE = Treat*nonshoulder*EventDay
g byte NonPeakshoulder = 1 - max(Peak,shoulder)
*g byte TNPsE = Treat*NonPeakshoulder*EventDay
g byte elcar = inrange(stratgroups,7,12) // elbil merged on navn eller etternavn og adresse

* Endogeneous variables 
g byte Effektpris = (Treat == 1 & noncomplier == 0 & EventDay == 1 & Peak == 1) | (Treat == 0 & noncomplier == 1 & EventDay == 1 & Peak == 1)
g byte EffektprisNonPeak = (Treat == 1 & noncomplier == 0 & EventDay == 1 & Peak == 0) | (Treat == 0 & noncomplier == 1 & EventDay == 1 & Peak == 0)
g byte Effektprisnext = (Treat == 1 & noncomplier == 0 & days_after == 1 & Peak == 1) | (Treat == 0 & noncomplier == 1 & days_after == 1 & Peak == 1)
g byte EffektprisNonPeaknext = (Treat == 1 & noncomplier == 0 & days_after == 1 & Peak == 0) | (Treat == 0 & noncomplier == 1 & days_after == 1 & Peak == 0)
g byte Effektprisshoulder = (Treat == 1 & noncomplier == 0 & EventDay == 1 & shoulder == 1) | (Treat == 0 & noncomplier == 1 & EventDay == 1 & shoulder == 1)


foreach y in TPE TNPE TPnextdays TNPnextdays Peak TsE shoulder Effektpris EffektprisNonPeak Effektprisnext EffektprisNonPeaknext Effektprisshoulder {
g byte `y'elcar = `y'*elcar 
}

save "data\out\estsample.dta", replace 

use "data\out\estsample.dta", clear 
keep maalepktnr 
duplicates drop 
save "data\out\estsample_maalepktnr.dta", replace 